import Mock from 'mockjs'
let List = []  //全局list 保存所有的数据
let id = 1;
for (var i = 0; i < 100; i++) {
    List.push(Mock.mock({
        id: id++,
        name: Mock.Random.cname(),
        age: Mock.Random.integer(18, 30),
        major: 'html5',
        address: Mock.Random.county(true),
        pay: Mock.Random.integer(0, 1),
        graduateTime: Mock.Random.date('yyyy-MM-dd')

    }))
}

// console.log(44, List)
export default {
    getTable: (config) => {
        // console.log(JSON.parse(config.body))
        //搜索逻辑
        const { searchVal, page, pageSize } = JSON.parse(config.body)
        //分页逻辑 mock    56
        // page=1 pageSize=10
        // page=2  pageSiz=10
        // page=3  pageSiz=10
        //公式：     (page-1)*pageSize  <= index< page* pageSize   0----9
        let newList1 = List.filter(item => {
            return item.name.indexOf(searchVal) != -1
        })
        let newList2 = newList1.filter((item, index) => {
            return (page - 1) * pageSize <= index && index < page * pageSize
        })                                       //10 ----- 19
        // 20 ---- 29
        return {
            code: 200,
            msg: '',
            data: newList2,  //10条数
            total: List.length
        }
    },
    editData: (config) => {
        console.log(config)
        let { id, name, age, pay, major, address, graduateTime } = JSON.parse(config.body)
        List.forEach(item => {
            if (item.id == id) {
                item.name = name;
                item.age = age;
                item.pay = pay;
                item.major = major;
                item.address = address;
                item.graduateTime = graduateTime
            }
        })
        return {
            code: 200,
            msg: '修改成功',
            data: null
        }
    },
    deleteData: (config) => {
        //console.log(config);
        let id = config.url.split('?')[1].split('=')[1]
        let newList = List.filter((item) => {
            return item.id != id
        })
        List = newList
        return {
            code: 200,
            msg: "删除成功",
            data: null
        }
    }

}